﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace zmMoniter
{
    public partial class frmArtificialUser : DevExpress.XtraEditors.XtraForm
    {
        private bool bIsAdd = true;
        private string sTitle = "添加人工振捣用户";
        private int iID = -1;
        private mainForm main = null;
        public frmArtificialUser(mainForm frm)
        {
            InitializeComponent();
            bIsAdd=true;
            main = frm;
        }

        private void sBtn_OK_Click(object sender, EventArgs e)
        {
            if (this.tEdt_UserName.Text == "")
            {
                label_Msg.Text = "请输入用户姓名！";
                
                return;
            }
            if (this.tEdt_RFID.Text == "")
            {
                label_Msg.Text = "请输入工号！";
                
                return;
            }
            if (bIsAdd)
                createUser();
            else
                upDateUser();
        }
        public void setModel(bool bFlag)
        {
            bIsAdd = bFlag;
            if (bIsAdd == true)
            {
                this.Text = "添加人工振捣用户";
                sTitle = "添加人工振捣用户";
            }
            else
            {
                this.Text = "编辑人工振捣用户信息";
                sTitle = "编辑人工振捣用户信息";
            }
        }
        public void setUpdateID(int id)
        {
            iID = id;
        }
        private bool createUser()
        {
            bool bRet=false;

            DateTime now = System.DateTime.Now;
            
            string sSQL = "INSERT INTO 人工振捣基本信息表(编号,RFID,姓名,联系方式,负责部位,是否在线," +
             "所在部门,创建时间)" +
             "VALUES (人工振捣用户自增长.nextval" + ",'" + tEdt_RFID.Text + "','" + tEdt_UserName.Text + "','" +tEdt_Tel.Text +
             "','" + tEdit_Position.Text + "',0,'" + tEdt_Department.Text +
             "',to_date('" + now.ToString("yyyy/MM/dd hh:mm:ss") + "','YYYY/MM/DD HH24:MI:SS'))";
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();
            int iResult = db.ExecuteSQL(sSQL);
            if (iResult > 0)
            {
                reSetEdit();
                bRet = true;
                
                this.Hide();
                main.fmArtificialUserList.UpdateDataList();
            }
            else
                label_Msg.Text = "添加人工振捣用户失败！";
                
            db.CloseConn();

            return bRet;
        }
        private bool upDateUser()
        {
            bool bRet = false;
            if (iID == -1)
                return bRet;
            PublicDataCls db = new PublicDataCls();
            db.OpenConn();

            string sSQL = "UPDATE 人工振捣基本信息表 SET RFID='" + tEdt_RFID.Text + "',姓名='" + tEdt_UserName.Text +
                "',联系方式='" + tEdt_Tel.Text + "',负责部位='" + tEdit_Position.Text + "',所在部门='" + tEdt_Department.Text+
                "' WHERE 编号=" + iID;

            int iResult = db.ExecuteSQL(sSQL);
            if (iResult > 0)
            {
                reSetEdit();
                bRet = true;
                this.Hide();
                main.fmArtificialUserList.UpdateDataList();
            }
            else
                label_Msg.Text = "保存人工振捣用户信息失败！";
                
            db.CloseConn();
            return bRet;
        }
        private void reSetEdit()
        {
            tEdt_RFID.Text = "";
            tEdt_UserName.Text = "";
            tEdt_Tel.Text = "";
            tEdit_Position.Text = "";
            tEdt_Department.Text = "";
            setModel(true);
        }
        public void setData(string sRFID,string sUserName,string sTel,string sPos,string sDepart)
        {
            tEdt_RFID.Text = sRFID;
            tEdt_UserName.Text = sUserName;
            tEdt_Tel.Text = sTel;
            tEdit_Position.Text = sPos;
            tEdt_Department.Text = sDepart;
        }
    }
}